package com.mycat.tencent.server;

import java.util.Map;

import com.mycat.entity.WxpayParam;
import com.mycat.exception.PaymentException;
import com.mycat.tencent.config.ConfigInfo;
import com.mycat.tencent.entity.request.HttpRequestParams;
import com.mycat.tencent.entity.request.WxAppPaymentParams;
import com.mycat.tencent.entity.request.WxUnifiedorderParams;
import com.mycat.tencent.entity.response.HttpResponseParam;
import com.mycat.tencent.entity.response.WxPrepayResponse;
import com.mycat.tencent.http.WxHttp;
import com.mycat.tencent.util.Sign;
import com.mycat.tencent.util.Utils;
import com.mycat.util.parser.api.Parser;
import com.mycat.util.parser.xml.XmlParser;

/**
 * @desc 微信支付服务
 * @author roger
 * @date 2016年9月26日
 */
public class WxPayService {

    private static Parser parser;

    static {
        parser = new XmlParser();
    }

    private WxPayService() {
    }

    /**
     * 统一下单
     *
     * @param wxUnifiedorderParams 统一下单参数
     * @return WxPrepayResponse
     */
    public static WxPrepayResponse getPrePayUnifiedorder(WxUnifiedorderParams wxUnifiedorderParams, String payWay) throws Exception {
        try {
            wxUnifiedorderParams.setAppid(ConfigInfo.appid);
            wxUnifiedorderParams.setMch_id(ConfigInfo.mch_id);
            wxUnifiedorderParams.setNonce_str(Utils.createSonceStr());
            wxUnifiedorderParams.setBody(ConfigInfo.body);// 描述
            wxUnifiedorderParams.setNotify_url(ConfigInfo.notify_url + payWay); // 通知地址
            wxUnifiedorderParams.setSpbill_create_ip("221.237.119.44");
            // 将统一下单参数转化为Map
            Map<String, Object> params = wxUnifiedorderParams.toMap();
            // 获取签名
            String sign = Sign.createSign(params, ConfigInfo.sing_key);
            params.put("sign", sign);
            // 拼装请求参数为XML
            String requestParam = Utils.toXmlStr(params);

            // 进行Http请求获取预支付号
            HttpRequestParams httpRequestParams = new HttpRequestParams();
            httpRequestParams.setUri(ConfigInfo.pay_unifiedorder);
            httpRequestParams.setRequestParams(requestParam);
            // 进行请求
            HttpResponseParam httpResponseParam = WxHttp.getInstance().post(httpRequestParams);

            WxPrepayResponse response = parser.from(httpResponseParam.getContent(), WxPrepayResponse.class);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PaymentException("SDK出现异常");
        }
    }

    /**
     * 返回调起支付请求参数字符串
     *
     * @param wxPrepayResponse WxPrepayResponse
     * @param wxPayWay         微信支付方式
     * @return 参数字符串
     * @throws Exception
     */
    public static WxpayParam getPaymentParam(WxPrepayResponse wxPrepayResponse, String wxPayWay) throws Exception {
    	WxpayParam paymentParam = new WxpayParam();
        try {
        	// 微信APP支付
            if ("APP".equalsIgnoreCase(wxPayWay)) {
                // 设置参数
                long timestamp = System.currentTimeMillis() / 1000;
                WxAppPaymentParams wxAppPaymentParams = new WxAppPaymentParams();
                wxAppPaymentParams.setAppid(wxPrepayResponse.getAppid());
                wxAppPaymentParams.setPrepayid(wxPrepayResponse.getPrepay_id());
                wxAppPaymentParams.setPartnerid(wxPrepayResponse.getMch_id());
                // 转换为Map
                Map<String, Object> params = wxAppPaymentParams.toMap();
                params.put("package", "Sign=WXPay");
                params.put("timestamp", String.valueOf(timestamp));
                params.put("noncestr", Utils.createSonceStr());
                // 获取签名
                String sign = Sign.createSign(params, ConfigInfo.sing_key);
                params.put("sign", sign);


                paymentParam.setTimestamp(String.valueOf(params.get("timestamp")));
                paymentParam.setAppid(String.valueOf(params.get("appid")));
                paymentParam.setNoncestr(String.valueOf(params.get("noncestr")));
                paymentParam.setPartnerid(String.valueOf(params.get("partnerid")));
                paymentParam.setPrepayid(String.valueOf(params.get("prepayid")));
                paymentParam.setSign(sign);
                return paymentParam;
            } else if ("JSAPI".equalsIgnoreCase(wxPayWay)) {

            }else{

            }
            return paymentParam;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PaymentException("SDK出现异常");
        }
    }

}
